﻿namespace Core.Threading.ThreadPools.Statistics
{
    public sealed class StatisticController : IStatisticController
    {
        private readonly object _locker = new object();
        private readonly int _maxThreads;
        private int _taskItemsQueued;
        private int _taskItemsStarted;

        public StatisticController(int maxThreads)
        {
            _maxThreads = maxThreads;
        }

        #region IStatisticController Members

        public void WorkItemStarted()
        {
            lock (_locker)
            {
                _taskItemsStarted++;
                _taskItemsQueued--;
            }
        }

        public void WorkItemQueued()
        {
            lock (_locker)
            {
                _taskItemsQueued++;
            }
        }

        public void WorkItemCompleted()
        {
            lock (_locker)
            {
                _taskItemsStarted--;
            }
        }

        public ExtendedThreadPoolStats GetStatistic(int consumersWaiting)
        {
            lock (_locker)
            {
                return new ExtendedThreadPoolStats(_taskItemsQueued, _taskItemsStarted, consumersWaiting, _maxThreads);
            }
        }

        #endregion
    }
}